package com.blb.service.admin.impl;

import com.blb.dao.admin.ProductTypeDao;
import com.blb.dao.admin.impl.ProductTypeDaoImpl;
import com.blb.pojo.ProductType;
import com.blb.pojo.Page;
import com.blb.service.admin.IProductTypeService;
import com.blb.utils.ApplicationContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductTypeServiceImpl implements IProductTypeService {

    @Autowired
    private ProductTypeDao productTypeDao;

    @Override
    public Page<ProductType> page(int page, int size) throws Exception {
        List<ProductType> list = productTypeDao.selectPage(page, size);
        int count = productTypeDao.count();
        return new Page<ProductType>(page, size, list, count);
    }

    @Override
    public void add(ProductType productType) throws Exception {
        productTypeDao.insert(productType);
    }

    @Override
    public void deleteById(int id) throws Exception {
        productTypeDao.deleteById(id);
    }

    @Override
    public void batchDelete(String[] ids) throws Exception {
        productTypeDao.batchDelete(ids);
    }

    @Override
    public ProductType getById(int id) throws Exception {
        return productTypeDao.selectById(id);
    }

    @Override
    public void update(ProductType productType) throws Exception {
        ProductType productType1 = productTypeDao.selectByName(productType.getName());
        if (productType1 != null && productType1.getId()!=productType.getId()){
            throw new Exception("此顺序已经存在");
        }
        productTypeDao.update(productType);
    }
    @Override
    public List<ProductType> all() throws Exception {
        return productTypeDao.selectAll();
    }
}
